#include "stdafx.h"
#include "msgbox.h"


using namespace ui;

void ShowMsgBox(HWND hwnd, const std::wstring& content, const std::wstring& title, const std::wstring& yes, const std::wstring& no, MsgboxCallback cb, const std::wstring& ignore)
{
	MsgBox* msgbox = new MsgBox(hwnd);
	HWND hWnd = msgbox->Create(hwnd, L"", WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, 0);
	if (hWnd == NULL)
		return;
	MutiLanSupport *multilan = MutiLanSupport::GetInstance();
	msgbox->SetTitle(title);
	msgbox->SetContent(content);
	msgbox->SetButton(yes, no, ignore);
	msgbox->Show(cb);
}

void ShowInputBox(HWND hwnd, const std::wstring& content, const std::wstring& title, const std::wstring& yes, const std::wstring& no, InputboxCallback cb, const std::wstring& ignore)
{
	MsgBox* msgbox = new MsgBox(hwnd);
	HWND hWnd = msgbox->Create(hwnd, L"", WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX, 0);
	if (hWnd == NULL)
		return;

	RichEdit *re = (RichEdit*)msgbox->FindControl(L"content");
	if (re)
	{
		re->SetClass(L"edit");
		re->SetFixedHeight(38);
		re->SetEnabled(true);
		re->SetReadOnly(false);
		re->SetFocus();
	}

	MutiLanSupport* multilan = MutiLanSupport::GetInstance();
	msgbox->SetInputMode(true);
	msgbox->SetTitle(title);
	msgbox->SetContent(content);
	msgbox->SetButton(yes, no, ignore);
	msgbox->ShowInput(cb);
}

const LPCTSTR MsgBox::kClassName = L"MsgBox";

MsgBox::MsgBox(HWND hWndParent) : hwnd_parent_(hWndParent)
{
}

MsgBox::~MsgBox()
{
}

std::wstring MsgBox::GetSkinFolder()
{
	return L"msgbox";
}

std::wstring MsgBox::GetSkinFile()
{
	return L"msgbox.xml";
}

std::wstring MsgBox::GetZIPFileName() const
{
	return (L"msgbox.zip");
}

std::wstring MsgBox::GetWindowClassName() const
{
	return kClassName;
}

std::wstring MsgBox::GetWindowId() const
{
	return kClassName;
}

UINT MsgBox::GetClassStyle() const
{
	return (UI_CLASSSTYLE_FRAME | CS_DBLCLKS);
}

LRESULT MsgBox::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	//if(uMsg == WM_DESTROY)
	//{
	//	HWND hWndParent = GetWindowOwner(m_hWnd);
	//	if(hWndParent)
	//	{
	//		::EnableWindow(hWndParent, TRUE);
	//		::SetForegroundWindow(hWndParent);
	//		::SetFocus(hWndParent);
	//	}
	//}
	return __super::HandleMessage(uMsg, wParam, lParam);
}

void MsgBox::OnEsc(BOOL &bHandled)
{
	bHandled = TRUE;
	EndMsgBox(MsgBoxRet::MB_CANCLE);
}
void MsgBox::Close(UINT nRet)
{
	if (hwnd_parent_)
	{
		::EnableWindow(hwnd_parent_, TRUE);
		::SetFocus(hwnd_parent_);
	}

	__super::Close(nRet);
}

void MsgBox::InitWindow()
{
	m_pRoot->AttachBubbledEvent(ui::kEventClick, nbase::Bind(&MsgBox::OnClicked, this, std::placeholders::_1));

	title_ = (Label*)FindControl(L"title");
	content_ = (RichEdit*)FindControl(L"content");
	btn_yes_ = (Button*)FindControl(L"btn_yes");
	btn_no_ = (Button*)FindControl(L"btn_no");
}

std::wstring MsgBox::GetContent()
{
	return content_->GetText();
}

bool MsgBox::OnClicked(ui::EventArgs* msg)
{
	std::wstring name = msg->pSender->GetName();
	if (name == L"btn_yes")
	{
		EndMsgBox(MsgBoxRet::MB_YES);
	}
	else if (name == L"btn_no")
	{
		EndMsgBox(MsgBoxRet::MB_NO);
	}
	else if (name == L"closebtn")
	{
		EndMsgBox(MsgBoxRet::MB_CANCLE);
	}
	else if (name == L"btn_ignore")
	{
		EndMsgBox(MsgBoxRet::MB_IGNORE);
	}
	return true;
}

void MsgBox::SetInputMode(bool bInput)
{
	inputMode_ = bInput;
}

void MsgBox::SetTitle(const std::wstring &str)
{
	title_->SetText(str);
}

void MsgBox::SetContent(const std::wstring &str)
{
	content_->SetText(str);

	int width = content_->GetFixedWidth();

	ui::CSize sz = content_->GetNaturalSize(width, 0);
	content_->SetFixedHeight(sz.cy, false);
}

void MsgBox::SetButton(const std::wstring& yes, const std::wstring& no, const std::wstring& ignore)
{
	if (!yes.empty())
	{
		btn_yes_->SetText(yes);
		btn_yes_->SetVisible(true);
		btn_yes_->SetFocus();
	}
	else
	{
		btn_yes_->SetVisible(false);
	}

	if (!no.empty())
	{
		btn_no_->SetText(no);
		btn_no_->SetVisible(true);
		if (yes.empty())
			btn_no_->SetFocus();
	}
	else
	{
		btn_no_->SetVisible(false);
	}
}

void MsgBox::Show(MsgboxCallback cb)
{
	msgbox_callback_ = cb;

	if (hwnd_parent_)
	{
		::EnableWindow(hwnd_parent_, FALSE);
	}
	
	CenterWindow();
	ShowWindow(true);
}

void MsgBox::ShowInput(InputboxCallback cb)
{
	inputbox_callback_ = cb;

	if (hwnd_parent_)
	{
		::EnableWindow(hwnd_parent_, FALSE);
	}

	CenterWindow();
	ShowWindow(true);
}

void MsgBox::EndMsgBox(MsgBoxRet ret)
{
	if (!inputMode_ || !inputbox_callback_)
		this->Close(0);

	if (msgbox_callback_)
	{
		shared::Post2UI(nbase::Bind(msgbox_callback_, ret));
	}
	else if (inputbox_callback_)
	{
		std::wstring sText = content_->GetText();
		if (inputbox_callback_(ret, sText))
			this->Close(0);
		//shared::Post2UI(nbase::Bind(inputbox_callback_, ret, sText));
	}
}
